<template>
    <text> {{label || text}}</text>
</template>

<script>
    export default {
        props: {
          // 组的编码
            type: {
                default: null
            },
          // 字典id
            val: {
                default: null
            },
          // 默认值
            text: {
                default: ''
            }
        },
        data() {
            return {
                label: null
            }
        },
        created() {
            this.setLabel(this.type, this.val)
        },
        watch: {
            type(type) {
                this.setLabel(type, this.val)
            },
            val(val) {
                this.setLabel(this.type, val)
            }
        },
        methods: {
            async setLabel(type, val) {
                if (type && val) {
                    let dict = uni.getStorageSync('dict_' + type)
                    if (!dict) {
                        const {data: res} = await this.$http.get('/dict/app/getItems?groupCode=' + type)
                        dict = res || []
                        if (dict && dict.length > 0) {
                            uni.setStorageSync('dict_' + type, dict)
                        }
                    }
                    dict.forEach(item => {
                        if (val === item.id) {
                            this.label = item.name
                            return
                        }
                    })
                }
            }
        }
    }
</script>
<style>
</style>
